package com.shj.service.impl;

import com.shj.dao.SysMenuMapper;
import com.shj.entity.SysMenu;
import com.shj.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 权限service
 * @Author songhuajian
 * @Date 2020/1/12 23:00
 **/
@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * @description 根据路径查询权限
     * @author songhuajian
     * @date 2020/1/12 23:14
     */
    @Override
    public SysMenu findByPath(String resourcePath){
        return sysMenuMapper.selectByUrl(resourcePath);
    }

    /**
     * @Description 查找全部
     * @author songhuajian
     * @date 2020/5/3 0:35
     */
    @Override
    public List<SysMenu> findAll() {
        return sysMenuMapper.findAll();
    }

    /**
     * @Description 构建根节点为menuId的菜单树
     * @author songhuajian
     * @date 2020/5/3 0:35
     */
    @Override
    public List<SysMenu> findChildren(String menuPid) {
        return sysMenuMapper.findChildren(menuPid);
    }

    /**
     * @Description 自带生成菜单编号
     * @author songhuajian
     * @date 2020/5/9 21:17
     */
    @Override
    public String maxMenuId(String menuPid) {
        String maxCode = sysMenuMapper.maxMenuId(menuPid);
        if(maxCode!=null){
            int beginIndex=maxCode.length()-2;
            String no=maxCode.substring(beginIndex);
            Integer number=Integer.parseInt(no);
            ++number;
            //使用0补足位数
            no=String.format("%02d", number);
            maxCode=menuPid+no;
        }else{
            maxCode=menuPid+"01";
        }
        return maxCode;
    }

    @Override
    public int add(SysMenu sysMenu) {
        // 自动生成菜单编号
        String menuId = maxMenuId(sysMenu.getMenuPid());
        sysMenu.setMenuId(menuId);
        return sysMenuMapper.insert(sysMenu);
    }

    @Override
    public int update(SysMenu sysMenu) {
        return sysMenuMapper.update(sysMenu);
    }

    @Override
    public int delete(String menuId) {
        return sysMenuMapper.delete(menuId);
    }

    @Override
    public SysMenu findByMenuId(String menuId) {
        return sysMenuMapper.selectByMenuId(menuId);
    }

}
